//
//  SceneDelegate.swift
//  rdb-led-banner
//
//  Created by radiobox3 on 2025/4/17.
//

import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?
    var screenWindow: UIWindow?
    
    //是否为第一次访问
    var isFirstLaunch: Bool = false

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        guard let _ = (scene as? UIWindowScene) else { return }
        
        self.isFirstLaunch = !RdbUserDefaults.boolValue(for: .rdbFirstLaunch)
        self.window?.overrideUserInterfaceStyle = .dark
        
        //self.showScreenView()
        self.showTestView()
    }
    
    func showScreenView(){
        //启动页
        guard let windowScene = (UIApplication.shared.connectedScenes.first as? UIWindowScene) else { return }
        self.screenWindow = UIWindow(windowScene: windowScene)
        self.screenWindow?.frame = CGRect(x: 0, y: 0, width: UIDevice.screenWidth(), height: UIDevice.screenHeight())
        let launchScreenController = RdbScreenLaunchController()
        self.screenWindow?.rootViewController = launchScreenController
        self.screenWindow?.windowLevel = (self.window?.windowLevel ?? .normal) + 1
        self.screenWindow?.makeKeyAndVisible()
        launchScreenController.completion = { [weak self] in
            guard let self = self else { return }
            if RdbSubscribeManager.shared.isSubscribed {
                self.showMainView()
            }else{
                self.showGuideView()
            }
        }
    }
    
    func showGuideView(){
        let guideController = RdbGuideController(transitionStyle: .scroll, navigationOrientation: .horizontal)
        
        guideController.closeAction = {[weak self] in
            guard let self = self else { return }
            RdbUserDefaults.update(value: true, keyPath: .rdbFirstLaunch)
            if RdbSubscribeManager.shared.isSubscribed {
                self.showMainView()
            }else{
                self.showRetentionView()
            }
        }
        
        self.screenWindow?.rootViewController = RdbNavigationController(rootViewController: guideController)
    }

    func showRetentionView(){
        let retentionController = RdbRetentionController()
        
        retentionController.closeAction = { [weak self] in
            guard let self = self else { return }
            if RdbSubscribeManager.shared.isSubscribed {
                self.showMainView()
            }else{
                self.showRetentionConfirmView()
            }
        }
        
        self.screenWindow?.rootViewController = RdbNavigationController(rootViewController: retentionController)
    }
    
    func showRetentionConfirmView(){
        let retentionConfirmController = RdbRetentionConfirmController()
        retentionConfirmController.closeAction = { [weak self] in
            guard let self = self else { return }
            self.showMainView()
        }
        self.screenWindow?.rootViewController?.present(retentionConfirmController, animated: true)
    }
    
    func showMainView(){
        self.window?.rootViewController = RdbTabberController()
        UIView.animate(withDuration: 0.25){
            self.screenWindow?.frame = CGRect(x: 0, y: UIDevice.screenHeight(), width: UIDevice.screenWidth(), height: UIDevice.screenHeight())
        }completion: { finished in
            self.screenWindow?.resignKey()
            self.screenWindow = nil
        }
    }
    
    func showTestView(){
        guard let windowScene = (UIApplication.shared.connectedScenes.first as? UIWindowScene) else { return }
        self.screenWindow = UIWindow(windowScene: windowScene)
        self.screenWindow?.frame = CGRect(x: 0, y: 0, width: UIDevice.screenWidth(), height: UIDevice.screenHeight())
        let viewController = RdbTabberController()
        self.screenWindow?.rootViewController = RdbNavigationController(rootViewController: viewController)
        self.screenWindow?.windowLevel = (self.window?.windowLevel ?? .normal) + 1
        self.screenWindow?.makeKeyAndVisible()
    }

    func sceneDidDisconnect(_ scene: UIScene) {
        // Called as the scene is being released by the system.
        // This occurs shortly after the scene enters the background, or when its session is discarded.
        // Release any resources associated with this scene that can be re-created the next time the scene connects.
        // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead).
    }

    func sceneDidBecomeActive(_ scene: UIScene) {
        // Called when the scene has moved from an inactive state to an active state.
        // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // Called when the scene will move from an active state to an inactive state.
        // This may occur due to temporary interruptions (ex. an incoming phone call).
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // Called as the scene transitions from the background to the foreground.
        // Use this method to undo the changes made on entering the background.
    }

    func sceneDidEnterBackground(_ scene: UIScene) {
        // Called as the scene transitions from the foreground to the background.
        // Use this method to save data, release shared resources, and store enough scene-specific state information
        // to restore the scene back to its current state.
    }


}

